﻿using C1.Win.C1FlexGrid;
using CWCS.BLL;
using CWCS.Entity.Robot;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace CWCS.UI
{
    public partial class FrmRobotManager :C1.Win.C1Ribbon.C1RibbonForm
    {
        private delegate void RichBoxCallBack(RichTextBox rb, string msg);
        private delegate void LabelCallBack(Label lb, bool b_ConnectStatus);
        private delegate void TextBoxCallBack(TextBox txtbox, string status);
        private delegate void DataGridViewCallBack(C1FlexGrid grd, List<BatteryInfo> lst);
        RobotBLL bll;
        public FrmRobotManager()
        {
            InitializeComponent();
            bll = new RobotBLL();
            if (bll.ZoneCode == "HC")
            {
                this.c1DockingTab1.TabPages.Remove(c1DockingTabPage1);
            }
            else
            {
                this.c1DockingTab1.TabPages.Remove(c1DockingTabPage2);
            }
            putRobotControl3.SetTextData("PickRobot");
            putRobotControl5.SetTextData("PickRobot");
            putRobotControl6.SetTextData("PickRobot");
            putRobotControl7.SetTextData("PickRobot");
            putRobotControl11.SetTextData("PickRobot");
            putRobotControl10.SetTextData("PickRobot");
            putRobotControl14.SetTextData("PickRobot");
            putRobotControl15.SetTextData("PickRobot");
            putRobotControl16.SetTextData("PickRobot");
            //新增换盘工位
            putRobotControl25.SetTextData("PickRobot");  // 7003
            putRobotControl26.SetTextData("PickRobot");  // 7004
            putRobotControl27.SetTextData("PutRobot");  // 7005
            putRobotControl28.SetTextData("PutRobot");  // 7006
        }
        private void FrmRobotManager_Load(object sender, EventArgs e)
        {
            bll.ShowRBMsg += new RobotBLL.ShowMessage(bsb_MsgNotify);
            bll.ShowRbStatus += new RobotBLL.NotiftyStatus(ShowRobotStatus);
            bll.Start();
        }
        private void ShowRobotStatus(RobotInfo rb)
        {
            if (rb != null)
            {

                if (rb.stationtype == 2 || rb.stationtype == 3 || rb.stationtype == 4|| rb.stationtype == 6)
                {
                    #region 化成段
                    if (rb.robotnum == "7001")//1008组盘
                    {
                        putRobotControl2.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7002")//1011组盘
                    {
                        putRobotControl1.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8001")//1720
                    {
                        putRobotControl6.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8002")//1723
                    {
                        putRobotControl7.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9001")//1640
                    {
                        putRobotControl5.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9002")//1647
                    {
                        putRobotControl3.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9003")//1644
                    {
                        putRobotControl4.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7003") // 拔钉换盘机器人1
                    {
                        putRobotControl25.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7004") // 拔钉换盘机器人2
                    {
                        putRobotControl26.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7005") // 拔钉换盘机器人3
                    {
                        putRobotControl27.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7006") // 拔钉换盘机器人4
                    {
                        putRobotControl28.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7007") // 化成换盘机器人1
                    {
                        putRobotControl30.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7008") // 化成换盘机器人2
                    {
                        putRobotControl31.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7009") // 化成换盘机器人3
                    {
                        putRobotControl32.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "7010") // 化成换盘机器人4
                    {
                        putRobotControl33.ShowRobotStatus(rb, rb.cellNum);
                    }
                    #endregion

                    #region 分容段
                    //if (rb.robotnum == "7003")//2117
                    //{
                    //    putRobotControl8.ShowRobotStatus(rb, rb.cellNum);
                    //}
                    //if (rb.robotnum == "7004")//2120
                    //{
                    //    putRobotControl13.ShowRobotStatus(rb, rb.cellNum);
                    //}

                    if (rb.robotnum == "8003")//2605
                    {
                        putRobotControl15.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8004")//2608
                    {
                        putRobotControl16.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8005")//分容拆盘1
                    {
                        putRobotControl35.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8006")//分容拆盘2
                    {
                        putRobotControl36.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8007")//常温组盘1
                    {
                        putRobotControl37.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "8008")//常温组盘2
                    {
                        putRobotControl38.ShowRobotStatus(rb, rb.cellNum);
                    }

                    if (rb.robotnum == "9004")//2147组盘
                    {
                        putRobotControl18.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9005")//2135挑选
                    {
                        putRobotControl10.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9006")//2144挑选
                    {
                        putRobotControl11.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9007")//2545组盘
                    {
                        putRobotControl9.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9008")//2549组盘
                    {
                        putRobotControl12.ShowRobotStatus(rb, rb.cellNum);
                    }
                    if (rb.robotnum == "9009")//2553挑选
                    {
                        putRobotControl14.ShowRobotStatus(rb, rb.cellNum);
                    }
                    //if (rb.robotnum == "9010")//码盘
                    //{
                    //    putRobotControl17.ShowRobotStatus(rb, rb.cellNum);
                    //}
                    #endregion
                }
            }
        }
        private void bsb_MsgNotify(string type, string msg)
        {
            if (type == "Connect" || type == "Msg" || type == "Break")
            {
                ShowText(txtConnect, msg);
            }
            else if (type == "Alarm")
            {
                ShowText(txtAlarm, msg);
            }
            else
            {
                #region 化成机械手
                if (msg.Contains("机械手:7001"))
                {
                    putRobotControl2.ShowLog(msg);
                }
                if (msg.Contains("机械手:7002"))
                {
                    putRobotControl1.ShowLog(msg);
                }
                if (msg.Contains("机械手:8001"))
                {
                    putRobotControl6.ShowLog(msg);
                }
                if (msg.Contains("机械手:8002"))
                {
                    putRobotControl7.ShowLog(msg);
                }
                if (msg.Contains("机械手:9001"))
                {
                    putRobotControl5.ShowLog(msg);
                }
                if (msg.Contains("机械手:9002"))
                {
                    putRobotControl3.ShowLog(msg);
                }
                if (msg.Contains("机械手:9003"))
                {
                    putRobotControl4.ShowLog(msg);
                }
                // 新增拔钉换盘机器人日志（使用新的控件变量名）
                if (msg.Contains("机械手:7003"))
                {
                    putRobotControl25.ShowLog(msg);
                }
                if (msg.Contains("机械手:7004"))
                {
                    putRobotControl26.ShowLog(msg);
                }
                if (msg.Contains("机械手:7005"))
                {
                    putRobotControl27.ShowLog(msg);
                }
                if (msg.Contains("机械手:7006"))
                {
                    putRobotControl28.ShowLog(msg);
                }
                if (msg.Contains("机械手:7007"))
                {
                    putRobotControl25.ShowLog(msg);
                }
                if (msg.Contains("机械手:7008"))
                {
                    putRobotControl26.ShowLog(msg);
                }
                if (msg.Contains("机械手:7009"))
                {
                    putRobotControl27.ShowLog(msg);
                }
                if (msg.Contains("机械手:7010"))
                {
                    putRobotControl28.ShowLog(msg);
                }

                #endregion

                #region 分容机械手

                if (msg.Contains("机械手:8003"))
                {
                    putRobotControl15.ShowLog(msg);
                }
                if (msg.Contains("机械手:8004"))
                {
                    putRobotControl16.ShowLog(msg);
                }
                if (msg.Contains("机械手:8005"))
                {
                    putRobotControl35.ShowLog(msg);
                }
                if (msg.Contains("机械手:8006"))
                {
                    putRobotControl36.ShowLog(msg);
                }
                if (msg.Contains("机械手:8007"))
                {
                    putRobotControl37.ShowLog(msg);
                }
                if (msg.Contains("机械手:8008"))
                {
                    putRobotControl38.ShowLog(msg);
                }
                if (msg.Contains("机械手:9004"))
                {
                    putRobotControl18.ShowLog(msg);
                }
                if (msg.Contains("机械手:9005"))
                {
                    putRobotControl10.ShowLog(msg);
                }
                if (msg.Contains("机械手:9006"))
                {
                    putRobotControl11.ShowLog(msg);
                }
                if (msg.Contains("机械手:9007"))
                {
                    putRobotControl9.ShowLog(msg);
                }
                if (msg.Contains("机械手:9008"))
                {
                    putRobotControl12.ShowLog(msg);
                }
                if (msg.Contains("机械手:9009"))
                {
                    putRobotControl14.ShowLog(msg);
                }
                //if (msg.Contains("机械手:9010"))
                //{
                //    putRobotControl17.ShowLog(msg);
                //}
                #endregion

            }
        }

        private void ShowText(RichTextBox rb, string msg)
        {
            if (rb.InvokeRequired)
            {
                RichBoxCallBack rbcb = new RichBoxCallBack(ShowText);
                this.Invoke(rbcb, new object[] { rb, msg });
            }
            else
            {
                if (rb.TextLength > 5000)
                    rb.Clear();
                rb.AppendText(System.DateTime.Now + ": " + msg + "\r\n");
            }
        }

        private void ShowTime(Label lb, bool b_ConnectStatus)
        {
            if (lb.InvokeRequired)
            {
                LabelCallBack rbcb = new LabelCallBack(ShowTime);
                this.Invoke(rbcb, new object[] { lb, b_ConnectStatus });
            }
            else
            {
                if (b_ConnectStatus)
                    lb.BackColor = lb.BackColor == Color.Lime ? Color.DarkGray : Color.Lime;
                else
                    lb.BackColor = Color.Red;
                lb.Text = b_ConnectStatus ? "已连接" : "断开";
            }
        }

        private void ShowRunStatus(TextBox tb, string runstatus)
        {
            if (tb.InvokeRequired)
            {
                TextBoxCallBack tbcb = new TextBoxCallBack(ShowRunStatus);
                this.Invoke(tbcb, new object[] { tb, runstatus });
            }
            else
            {
                tb.Text = runstatus;
            }
        }
    }
}
